<template>
  <p-modal
    :title="title"
    :width="1200"
    :visible="visible"
    :maskClosable="false"
    :confirmLoading="confirmLoading"
    @ok="handleOk"
    @cancel="handleCancel">
    <p-spin :spinning="confirmLoading">
      <!-- 主表单区域 -->
      <p-form :form="form">
        <p-row>

	  <p-col :span="12">
		<p-form-item label="工站编码" :labelCol="labelCol" :wrapperCol="wrapperCol">
			<p-input   v-decorator="[ 'workstationcode', validatorRules.workstationcode]" placeholder="请输入工站编码"></p-input>
		</p-form-item>
      </p-col>
	  <p-col :span="12">
		<p-form-item label="工站名称" :labelCol="labelCol" :wrapperCol="wrapperCol">
			<p-input   v-decorator="[ 'workstationname', validatorRules.workstationname]" placeholder="请输入工站名称"></p-input>
		</p-form-item>
      </p-col>
	  <p-col :span="12">
		<p-form-item label="工序页次" :labelCol="labelCol" :wrapperCol="wrapperCol">
			<p-input-number   v-decorator="[ 'processPage', validatorRules.processPage]" placeholder="请输入工序页次" style="width: 100%"/>
		</p-form-item>
      </p-col>
	  <p-col :span="12">
		<p-form-item label="是否加载线材/组件清单" :labelCol="labelCol" :wrapperCol="wrapperCol">
			<j-dict-select-tag  type="radio" v-decorator="['module', validatorRules.module]" :trigger-change="true" dictCode="ba_yn" placeholder="请选择是否加载线材/组件清单"/>
		</p-form-item>
      </p-col>
	  <p-col :span="12">
		<p-form-item label="物料描述" :labelCol="labelCol" :wrapperCol="wrapperCol">
			<p-input   v-decorator="[ 'mdescription', validatorRules.mdescription]" placeholder="请输入物料描述"></p-input>
		</p-form-item>
      </p-col>
	  <p-col :span="12">
		<p-form-item label="是否单页图片" :labelCol="labelCol" :wrapperCol="wrapperCol">
			<j-dict-select-tag  type="radio" v-decorator="['cutForm', validatorRules.cutForm]" :trigger-change="true" dictCode="ba_yn" placeholder="请选择是否单页图片"/>
		</p-form-item>
      </p-col>
	  <p-col :span="12">
		<p-form-item label="物料编码" :labelCol="labelCol" :wrapperCol="wrapperCol">
			<f-popup
					contextPath="basic-business"
					v-decorator="['mcode', validatorRules.mcode]"
					:trigger-change="true"
					org-fields="id,mcode,mname,sprc,mexplain"
					dest-fields="mid,mcode,mname,sprc,mexplain"
					code="wms_cpinfo"
					@callback="popupCallback"/>
		</p-form-item>
      </p-col>
	  <p-col :span="12">
		<p-form-item label="物料名称" :labelCol="labelCol" :wrapperCol="wrapperCol">
			<p-input readOnly  v-decorator="[ 'mname', validatorRules.mname]" placeholder="请输入物料名称"></p-input>
		</p-form-item>
      </p-col>
	  <p-col :span="12">
		<p-form-item label="物料规格型号" :labelCol="labelCol" :wrapperCol="wrapperCol">
			<p-input readOnly  v-decorator="[ 'sprc', validatorRules.sprc]" placeholder="请输入物料规格型号"></p-input>
		</p-form-item>
      </p-col>
	  <p-col :span="12">
		<p-form-item label="物料描述" :labelCol="labelCol" :wrapperCol="wrapperCol">
			<p-input readOnly  v-decorator="[ 'mexplain', validatorRules.mexplain]" placeholder="请输入物料描述"></p-input>
		</p-form-item>
      </p-col>

        </p-row>
      </p-form>

      <!-- 子表单区域 -->
      <p-tabs v-model="activeKey" @change="handleChangeTabs">
        <p-tab-pane tab="工装设备" :key="refKeys[0]" :forceRender="true">
          <j-editable-table
            :ref="refKeys[0]"
            :loading="mesPeEquipmentTable.loading"
            :columns="mesPeEquipmentTable.columns"
            :dataSource="mesPeEquipmentTable.dataSource"
            :maxHeight="300"
            :rowNumber="true"
            :rowSelection="true"
            :actionButton="true"/>
        </p-tab-pane>
        
        <p-tab-pane tab="操作步骤&注意事项" :key="refKeys[1]" :forceRender="true">
          <j-editable-table
            :ref="refKeys[1]"
            :loading="mesPeMattersNeedAttentionTable.loading"
            :columns="mesPeMattersNeedAttentionTable.columns"
            :dataSource="mesPeMattersNeedAttentionTable.dataSource"
            :maxHeight="300"
            :rowNumber="true"
            :rowSelection="true"
            :actionButton="true"/>
        </p-tab-pane>
        
        <p-tab-pane tab="工站SOP制作图片" :key="refKeys[2]" :forceRender="true">
          <j-editable-table
            :ref="refKeys[2]"
            :loading="mesPeSopMakePictureTable.loading"
            :columns="mesPeSopMakePictureTable.columns"
            :dataSource="mesPeSopMakePictureTable.dataSource"
            :maxHeight="300"
            :rowNumber="true"
            :rowSelection="true"
            :actionButton="true"/>
        </p-tab-pane>
        
        <p-tab-pane tab="工序BOM" :key="refKeys[3]" :forceRender="true">
          <j-editable-table
            :ref="refKeys[3]"
            :loading="mesPeProcedureBomTable.loading"
            :columns="mesPeProcedureBomTable.columns"
            :dataSource="mesPeProcedureBomTable.dataSource"
            :maxHeight="300"
            :rowNumber="true"
            :rowSelection="true"
            :actionButton="true"/>
        </p-tab-pane>
        
      </p-tabs>

    </p-spin>
  </p-modal>
</template>

<script>

  import pick from 'lodash.pick'
  import { FormTypes,getRefPromise } from '@/utils/JEditableTableUtil'
  import { JEditableTableMixin } from '@/mixins/JEditableTableMixin'
  import { validateDuplicateValue } from '@/utils/util'
  import JDictSelectTag from "@/components/dict/JDictSelectTag"

  export default {
    name: 'MesPeStationSopMakeModal',
    mixins: [JEditableTableMixin],
    components: {
      JDictSelectTag,
    },
    data() {
      return {
        labelCol: {
          span: 6
        },
        wrapperCol: {
          span: 16
        },
        labelCol2: {
          span: 3
        },
        wrapperCol2: {
          span: 20
        },
        // 新增时子表默认添加几行空数据
        addDefaultRowNum: 1,
        validatorRules: {
          workstationcode: {rules: [
            {required: true, message: '请输入工站编码!'},
          ]},
          workstationname: {rules: [
            {required: true, message: '请输入工站名称!'},
          ]},
          processPage: {rules: [
            {required: true, message: '请输入工序页次!'},
          ]},
          module: {rules: [
            {required: true, message: '请输入是否加载线材/组件清单!'},
          ]},
          mdescription: {rules: [
          ]},
          cutForm: {rules: [
            {required: true, message: '请输入是否单页图片!'},
          ]},
          mcode: {rules: [
            {required: true, message: '请输入物料编码!'},
          ]},
          mname: {rules: [
          ]},
          sprc: {rules: [
          ]},
          mexplain: {rules: [
          ]},
        },
        refKeys: ['mesPeEquipment', 'mesPeMattersNeedAttention', 'mesPeSopMakePicture', 'mesPeProcedureBom', ],
        tableKeys:['mesPeEquipment', 'mesPeMattersNeedAttention', 'mesPeSopMakePicture', 'mesPeProcedureBom', ],
        activeKey: 'mesPeEquipment',
        // 工装设备
        mesPeEquipmentTable: {
          loading: false,
          dataSource: [],
          columns: [
            {
              title: '资源名称 ',
              key: 'resourceName',
              type: FormTypes.input,
              width: '200px',
              placeholder: '请输入${title}',
              defaultValue: '',
              validateRules: [{ required: true, message: '${title}不能为空' }],
            },
            {
              title: '资源类型 ',
              key: 'resourceType',
              type: FormTypes.input,
              width: '200px',
              placeholder: '请输入${title}',
              defaultValue: '',
            },
            {
              title: '资源规格型号',
              key: 'resourceSprc',
              type: FormTypes.input,
              width: '200px',
              placeholder: '请输入${title}',
              defaultValue: '',
            },
            {
              title: '资源数量',
              key: 'resourceQuantity',
              type: FormTypes.input,
              width: '200px',
              placeholder: '请输入${title}',
              defaultValue: '',
            },
          ]
        },
        // 操作步骤&注意事项
        mesPeMattersNeedAttentionTable: {
          loading: false,
          dataSource: [],
          columns: [
            {
              title: '操作步骤',
              key: 'operatingSteps',
              type: FormTypes.input,
              width: '200px',
              placeholder: '请输入${title}',
              defaultValue: '',
            },
            {
              title: '图号',
              key: 'figureNumber',
              type: FormTypes.input,
              width: '200px',
              placeholder: '请输入${title}',
              defaultValue: '',
            },
            {
              title: '注意事项',
              key: 'matter',
              type: FormTypes.input,
              width: '200px',
              placeholder: '请输入${title}',
              defaultValue: '',
            },
          ]
        },
        // 工站SOP制作图片
        mesPeSopMakePictureTable: {
          loading: false,
          dataSource: [],
          columns: [
            {
              title: '图号',
              key: 'pictureOrder',
              type: FormTypes.input,
              width: '200px',
              placeholder: '请输入${title}',
              defaultValue: '',
            },
            {
              title: '图号地址',
              key: 'pictureUrl',
              type: FormTypes.input,
              width: '200px',
              placeholder: '请输入${title}',
              defaultValue: '',
            },
            {
              title: '类型',
              key: 'pictureType',
              type: FormTypes.input,
              width: '200px',
              placeholder: '请输入${title}',
              defaultValue: '',
            },
          ]
        },
        // 工序BOM
        mesPeProcedureBomTable: {
          loading: false,
          dataSource: [],
          columns: [
            {
              title: '物料编码',
              key: 'mcode',
              type: FormTypes.popup,
              contextPath: 'basic-business',
              popupCode: 'wms_cpinfo',
              destFields: 'mid,mcode,mname,sprc,mexplain',
              orgFieldse: 'id,mcode,mname,sprc,mexplain',
              width: '200px',
              placeholder: '请输入${title}',
              defaultValue: '',
              validateRules: [{ required: true, message: '${title}不能为空' }],
            },
            {
              title: '物料名称',
              key: 'mname',
              type: FormTypes.input,
              width: '200px',
              placeholder: '请输入${title}',
              defaultValue: '',
            },
            {
              title: '物料规格型号',
              key: 'sprc',
              type: FormTypes.input,
              width: '200px',
              placeholder: '请输入${title}',
              defaultValue: '',
            },
            {
              title: '物料描述',
              key: 'mexplain',
              type: FormTypes.input,
              width: '200px',
              placeholder: '请输入${title}',
              defaultValue: '',
            },
            {
              title: '数量',
              key: 'qty',
              type: FormTypes.input,
              width: '200px',
              placeholder: '请输入${title}',
              defaultValue: '',
            },
            {
              title: '备注',
              key: 'remarks',
              type: FormTypes.input,
              width: '200px',
              placeholder: '请输入${title}',
              defaultValue: '',
            },

            {
              title: 'mid',
              key: 'mid',
              type: 'hidden'
            },




          ]
        },
        url: {
          add: '/mes-craft/auto/mesPeStationSopMake/add',
          edit: '/mes-craft/auto/mesPeStationSopMake/edit',
          mesPeEquipment: {
            list: '/mes-craft/auto/mesPeStationSopMake/queryMesPeEquipmentByMainId'
          },
          mesPeMattersNeedAttention: {
            list: '/mes-craft/auto/mesPeStationSopMake/queryMesPeMattersNeedAttentionByMainId'
          },
          mesPeSopMakePicture: {
            list: '/mes-craft/auto/mesPeStationSopMake/queryMesPeSopMakePictureByMainId'
          },
          mesPeProcedureBom: {
            list: '/mes-craft/auto/mesPeStationSopMake/queryMesPeProcedureBomByMainId'
          },
        }
      }
    },
    methods: {
      getAllTable() {
        let values = this.tableKeys.map(key => getRefPromise(this, key))
        return Promise.all(values)
      },
      /** 调用完edit()方法之后会自动调用此方法 */
      editAfter() {
        let fieldval = pick(this.model,'workstationcode','workstationname','processPage','module','mdescription','cutForm','mcode','mname','sprc','mexplain')
        this.$nextTick(() => {
          this.form.setFieldsValue(fieldval)
        })
        // 加载子表数据
        if (this.model.id) {
          let params = { id: this.model.id }
          this.requestSubTableData(this.url.mesPeEquipment.list, params, this.mesPeEquipmentTable)
          this.requestSubTableData(this.url.mesPeMattersNeedAttention.list, params, this.mesPeMattersNeedAttentionTable)
          this.requestSubTableData(this.url.mesPeSopMakePicture.list, params, this.mesPeSopMakePictureTable)
          this.requestSubTableData(this.url.mesPeProcedureBom.list, params, this.mesPeProcedureBomTable)
        }
      },
      /** 整理成formData */
      classifyIntoFormData(allValues) {
        let main = Object.assign(this.model, allValues.formValue)

        return {
          ...main, // 展开
          mesPeEquipmentList: allValues.tablesValue[0].values,
          mesPeMattersNeedAttentionList: allValues.tablesValue[1].values,
          mesPeSopMakePictureList: allValues.tablesValue[2].values,
          mesPeProcedureBomList: allValues.tablesValue[3].values,
        }
      },
      validateError(msg){
        this.$message.error(msg)
      },
     popupCallback(row){
       this.form.setFieldsValue(pick(row,'workstationcode','workstationname','processPage','module','mdescription','cutForm','mcode','mname','sprc','mexplain'))
     },

    }
  }
</script>

<style scoped>
</style>